<?php
namespace Home\Controller;
use Think\Controller;
class ShoppingController extends Controller {

	public function __construct(){
			parent::__construct();
			if(!$_SESSION['user_is_login']){
				 $this->error('没有登录，请登录',U('home/user/login'));
			}
		}

  	public function addToCart(){
  		$user_id=$_SESSION['user_id'];
  		$product_id= I('post.product_id');
  		$num=I('post.num');
  		$where=array(
  			'user_id'=>$user_id,
  			'product_id'=>$product_id,
  			);

  		if($cart=M('user_cart')->where($where)->find()){
  			M('user_cart')->where($where)->save(array(
  				'num'=>$cart['num']+$num
  			));
  		}else{
  			M('user_cart')->add(array(
  				'user_id'=>$user_id,
  				'product_id'=>$product_id,
  				'num'=>$num,
  				));
  		}

  		$this->success('加入购物车成功');
  	}

 }
   
